package com.cuz.hot100;


import com.cuz.daileetcode.utils.TreeNode;

import java.util.LinkedList;

public class Test {

    TreeNode errorNodeX = null;
    TreeNode errorNodeY = null;
    TreeNode pre = null;

    public void recoverTree(TreeNode root) {
        if (root == null) {
            return;
        }
        scan(root);
        if (errorNodeX != null && errorNodeY != null) {
            int temp = errorNodeX.val;
            errorNodeX.val = errorNodeY.val;
            errorNodeY.val = temp;
        }
    }

    private void scan(TreeNode root) {
        if (root == null) {
            return;
        }

        if (errorNodeX != null && errorNodeY != null) {
            return;
        }

        scan(root.left);

        if (pre == null) {
            pre = root;
        } else {
            if (pre.val > root.val) {
                errorNodeX = pre;
                errorNodeY = root;
            }
            pre = root;
        }

        scan(root.right);
    }

    public static void main(String[] args) {
        TreeNode treeNode1 = new TreeNode(1);
        TreeNode treeNode3 = new TreeNode(3);
        TreeNode treeNode2 = new TreeNode(2);
        treeNode1.left=treeNode3;
        treeNode3.right =treeNode2;

        new Test().recoverTree(treeNode1);


    }
}
